package com.sikao.app.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.ChangeLoginPswSubmitBean;
import com.sikao.app.dao.ChangeLoginPswModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.StringUtils;
/**
 * 修改登录密码
 */
public class ChangeLoginPswActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private EditText originalPswEt;
	private EditText newPswEt1;
	private EditText newPswEt2;
	private Button changePswBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_change_login_psw);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {

	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		originalPswEt = (EditText) findViewById(R.id.original_psw_et);
		newPswEt1 = (EditText) findViewById(R.id.new_psw_et1);
		newPswEt2 = (EditText) findViewById(R.id.new_psw_et2);
		changePswBtn = (Button) findViewById(R.id.change_psw_btn);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		changePswBtn.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("修改登录密码");
		backIv.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.change_psw_btn:
				changePsw();
				break;
			default:
				break;
		}
	}

	private void changePsw() {

		if (!checkInput()) {
			return;
		}
		ChangeLoginPswSubmitBean bean = new ChangeLoginPswSubmitBean();
		bean.setOldPwd(originalPswEt.getText().toString());
		bean.setPwd(newPswEt1.getText().toString());
		bean.setAgapwd(newPswEt2.getText().toString());
		new ChangeLoginPswModel(this, new OnNetBackListener<Bean>() {
			@Override
			public void netBack(Bean data) {
				if (data == null) {
					return;
				}
				if (data.getStatus().equals("1")) {
					Toast.makeText(ChangeLoginPswActivity.this, "成功修改登录密码", Toast.LENGTH_SHORT).show();
					finish();
				} else {
					Toast.makeText(ChangeLoginPswActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		}).request(JSONUtil.toJSON(bean), true);
	}

	private boolean checkInput() {

		if (TextUtils.isEmpty(originalPswEt.getText().toString())) {
			Toast.makeText(this, "请输入原密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (TextUtils.isEmpty(newPswEt1.getText().toString())) {
			Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (originalPswEt.getText().toString().equals(newPswEt1.getText().toString())) {
			Toast.makeText(this, "原密码和新密码相同", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (newPswEt1.getText().toString().length() > 18 || newPswEt1.getText().toString().length() < 6) {
			Toast.makeText(this, "密码长度要求为6~18位", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!StringUtils.isLoginPsw(newPswEt1.getText().toString()) || !newPswEt1.getText().toString().matches("^[a-zA-Z].*$")) {
			Toast.makeText(this, "密码必须以字母开头,仅且同时包含数字和字母", Toast.LENGTH_SHORT).show();
			return false;
		}

		if (TextUtils.isEmpty(newPswEt2.getText().toString())) {
			Toast.makeText(this, "请再次输入新密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!(newPswEt1.getText().toString()).equals(newPswEt2.getText().toString())) {
			Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

}
